package com.michael.study.jetpack.module.browse

import android.os.Build
import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import com.michael.study.jetpack.databinding.ActivityBrowseBinding

class BrowseActivity : AppCompatActivity() {

    private lateinit var mBinding :ActivityBrowseBinding
    private lateinit var mWebView: WebView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityBrowseBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        mWebView = mBinding.webView
        initWebView()
    }

    private fun initWebView() {
        if (mWebView != null) {
            val webSettings: WebSettings = mWebView.getSettings()
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.javaScriptEnabled = true
            webSettings.setSupportZoom(true)
            webSettings.setAppCacheEnabled(false)
            webSettings.setGeolocationEnabled(true)
            webSettings.domStorageEnabled = true
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            }
            webSettings.javaScriptCanOpenWindowsAutomatically = true
            webSettings.setGeolocationEnabled(true)
            webSettings.useWideViewPort = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.databaseEnabled = true
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.loadWithOverviewMode = true
            webSettings.setUserAgentString(webSettings.userAgentString + "/ydbus")
            mWebView.setWebViewClient(WebViewClient())
            mWebView.setWebChromeClient(WebChromeClient())
            mWebView.loadUrl("file:///android_asset/jump.html")
        }
    }
}